package com.ymt.common.tools;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by zhanghao on 2017/8/5.
 */
public class JsonWrapper {
    private static ThreadLocal<ObjectMapper> mapper = new ThreadLocal<ObjectMapper>() {

        @Override
        protected ObjectMapper initialValue() {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.setDateFormat(format);
            return mapper;
        }

    };

    public static <T> T readValue(String json, Class<T> clazz) {
        if (StringUtils.isBlank(json) || clazz == null) {
            return null;
        }
        try {
            return mapper.get().readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T readValue(String json, TypeReference<T> typeReference) {
        if (StringUtils.isBlank(json)) {
            return null;
        }
        try {
            return (T) mapper.get().readValue(json, typeReference);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static List<?> readListValue(String json, Class<?> clazz) {
        if (StringUtils.isBlank(json) || clazz == null) {
            return null;
        }
        try {
            JavaType javaType = mapper.get().getTypeFactory().constructParametricType(List.class, clazz);
            //如果是Map类型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);
            List<?> list = mapper.get().readValue(json, javaType);
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据json串和节点名返回节点
     *
     * @param json
     * @param nodeName
     * @return
     */
    public static JsonNode getNode(String json, String nodeName) {
        JsonNode node;
        try {
            node = mapper.get().readTree(json);
            return node.get(nodeName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * JsonNode转换为Java泛型对象
     *
     * @param node
     * @param tr
     * @param <T>
     * @return
     */
    public static <T> T jsonNode2GenericObject(JsonNode node, TypeReference<T> tr) {
        if (node == null || "".equals(node)) {
            return null;
        } else {
            try {
                return (T) mapper.get().readValue(node.toString(), tr);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static String writeValue(Object obj) {
        if (obj == null) {
            return null;
        }
        try {
            StringWriter sw = new StringWriter();
            mapper.get().writeValue(sw, obj);
            return sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
